﻿using System;
using System.ComponentModel;
using System.Linq;
using ProjectEuler.Extensions;
using ProjectEuler.Linq;

namespace ProjectEuler.Problems
{
    [EulerProblem(22, 871198282)]
    [Description("What is the total of all the name scores in the file of first names?")]
    internal sealed class Problem022 : EulerProblem
    {
        public override Object Solve()
        {
            var names = ReadNames();
            Array.Sort(names, StringComparer.Ordinal);

            return names.Select((n, i) => (i + 1) * n.NumberValue()).Sum();
        }

        private String[] ReadNames()
        {
            using (var reader = OpenTextAttachment())
            {
                return reader.ReadAllLines().ToArray();
            }
        }
    }
}
